package com.servlet;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Properties;

import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class for Servlet: HelloServlet
 *
 */

	
 public class CallerServlet extends HttpServlet 
     implements Servlet {

	private static String filePath;
	 
	public CallerServlet() {
		super();
	}   	
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
	    throws ServletException, IOException {
		request.setAttribute("numbers", getNumbersByJudges());
		request.setAttribute("notes", getNotes());
		RequestDispatcher view = request.getRequestDispatcher("/caller.jsp");
        view.forward(request, response);
	}  	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, 
	     HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) 
	    throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
	
	public HashMap<String, ArrayList<String>> getNumbersByJudges() throws FileNotFoundException{
        //InputStream is = context.getResourceAsStream("/WEB-INF/files/numbers.txt");
		if(filePath==null)
			filePath=getFilePath();
		File file = new File(filePath+"numbers.txt");
		InputStream is = new FileInputStream(file);
        HashMap<String, ArrayList<String>> numbersForJudges = new HashMap<String, ArrayList<String>>();
        if (is != null) {
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader reader = new BufferedReader(isr);
            String text = "";             
            try {
				while ((text = reader.readLine()) != null) {
					//controllo che la riga non sia commentata
					if(!text.startsWith("//") && text.length()>0){
						String giudice = text.substring(0,text.indexOf("="));
						String numeri = text.substring(text.indexOf("=")+1);
						ArrayList<String> numeriList = new ArrayList<String>();
						String [] splitLine = numeri.split(",",-1);
				        for(String x : splitLine)
				        {
				            numeriList.add(x);
				        }
				        numbersForJudges.put(giudice, numeriList);						
					}
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
        }
        return numbersForJudges;
	}
	
	private String getFilePath(){
		Properties propertiesFile = new Properties();
		ServletContext context = getServletContext();
		File file = new File(context.getRealPath("/WEB-INF/files/location.properties"));
        FileInputStream fis=null;
		try {
			fis = new FileInputStream(file);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        try {
			propertiesFile.load(fis);
			fis.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        String path=propertiesFile.getProperty("path");
		return path;
	}
	
	private String getNotes() throws IOException{
		if(filePath==null)
			filePath=getFilePath();
		File file = new File(filePath+"notes.txt");
		InputStream is = new FileInputStream(file);
		String text = "";
		String notes="";
		if (is != null) {
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader reader = new BufferedReader(isr);

           notes = reader.readLine();
        }

        return notes;
				
	}
	
}